<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="right">
			<div class="sketch_content">
                <el-form-item label="物料名称" prop="material_name">
                    <el-input v-model="form.material_name" placeholder="请输入物料名称" clearable></el-input>
                </el-form-item>
                <el-form-item label="物料类别" prop="cate_id">
                    <el-select v-model="form.cate_id" placeholder="请选择" style="width:100%">
                            <el-option label="毛料" :value="1"></el-option>
                            <el-option label="辅料" :value="2"></el-option>
                            <el-option label="其他" :value="3"></el-option>
                        </el-select>
                </el-form-item>
                <el-form-item label="损耗" prop="loss_ratio">
                    <el-input v-model="form.loss_ratio" placeholder="请输入" type="number" clearable></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" placeholder="请输入" clearable></el-input>
                </el-form-item>
                <el-form-item label="是否禁用" prop="system_disable">
                    <el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
                </el-form-item>
			</div>
		</el-form>
		<template #footer>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
			<el-button @click="visible=false" >取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		components: {
			scEditor
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加物料',
					edit: '编辑物料',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
					material_name: "",
                    cate_id:"",
					sort:0,
                    loss_ratio:0,
					system_disable:0,
				},
				//验证规则
				rules: {
					material_name: [
						{required: true, message: '请输入物料名称'}
					],
					cate_id: [
						{required: true, message: '请选择类别'}
					],
				},
				cates: [],  //品牌
                Settlement:[],//结算方式
                muDan:[
                    {id:0,name:'否'},
                    {id:1,name:'是'},
                ],
			}
		},
		mounted() {
            this.getCate()
            this.getSettlement()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.id) {
							res = await this.$API.material.getMaterialList.edit.post(this.form);
						}else{
							res = await this.$API.material.getMaterialList.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                console.log('data :>> ', data);
				Object.assign(this.form, data)
			},
            // 获取品牌
            async getCate(){
				var res = await this.$API.customer.getBrand.lists.get();
				this.cates = res.data.rows;
			},
            // 获取结算方式
            async getSettlement(){
				var res = await this.$API.customer.getSettlement.get();
				this.Settlement = res.data;
			},
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
